package com.sakura.flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.sakura.flowers.entity.Category;
import com.sakura.flowers.service.CategoryService;
import com.sakura.flowers.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (Category)表控制层
 *
 * @author Sakura
 * @since 2023-11-26 17:08:39
 */
@Api(value = "/api/category", tags = {"(Category)表控制层"})
@RestController
@RequestMapping("/api/category")
@CrossOrigin
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param pageNo    当前页
     * @param pageSize  数据大小
     * @param searchKey 查询参数
     * @return 查询结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageNo", value = "当前页", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageSize", value = "数据大小", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "searchKey", value = "查询参数")
    })
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
    @GetMapping("/{pageNo}/{pageSize}")
    public R queryByPage(@PathVariable Integer pageNo,
                         @PathVariable Integer pageSize,
                         @RequestParam(required = false) String searchKey) {
        LambdaQueryWrapper<Category> categoryQueryWrapper = new LambdaQueryWrapper<>();
        categoryQueryWrapper.like(searchKey != null && searchKey != "", Category::getName, searchKey);
        Page<Category> page = categoryService.page(new Page<>(pageNo, pageSize), categoryQueryWrapper);
        return R.success(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "主键", required = true)
    })
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据", httpMethod = "GET")
    @GetMapping("{id}")
    public R<Category> queryById(@PathVariable("id") Long id) {
        return R.success(this.categoryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param category 实体
     * @return 新增结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Category", name = "category", value = "实体", required = true)
    })
    @ApiOperation(value = "新增数据", notes = "新增数据", httpMethod = "POST")
    @PostMapping
    public R add(@RequestBody Category category) {
        boolean result = this.categoryService.save(category);
        if (!result) {
            return R.error("添加失败");
        }
        return R.success("添加成功");
    }

    /**
     * 编辑数据
     *
     * @param category 实体
     * @return 编辑结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Category", name = "category", value = "实体", required = true)
    })
    @ApiOperation(value = "编辑数据", notes = "编辑数据", httpMethod = "PUT")
    @PutMapping
    public R edit(@RequestBody Category category) {
        boolean result = this.categoryService.updateById(category);
        if (!result) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "主键", required = true)
    })
    @ApiOperation(value = "删除数据", notes = "删除数据", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean result = this.categoryService.removeById(id);
        if (!result) {
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }

    @GetMapping("/select/list")
    public R getSelectList() {
        return R.success(categoryService.list());
    }

    @ApiOperation(value = "添加或者修改", notes = "添加或者修改")
    @Transactional
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody Category category) {
        category.setUpdateTime(new Date());
        //添加
        if (category.getId() == null || category.getId() == 0) {
            category.setCreateTime(new Date());
            boolean save = categoryService.save(category);
            if (!save) {
                return R.error("添加失败，请重新尝试");
            }
            return R.success("添加成功");
        }
        //修改
        boolean update = categoryService.updateById(category);
        if (!update) {
            return R.error("修改失败，请重新尝试");
        }
        return R.success("修改成功");
    }

}
